class GameSystemSD extends Behaviour {


    system = CollisionDetectionSystem;
    gameResult = false;
    full = [];
    unFull = [];


    onUpdate() {
        this.gameResult = this.checkResult();
        if (this.gameResult) {
            console.log("win");
        }
    }

    checkResult() {
        for (let j = 0; j < 5; j++) {
            for (let i = 0; i < 5; i++) {
                if (this.full[j][i] !== this.unFull[j][i]) {
                    return false;
                }
            }
        }
        return true;
    }
}

class GameInitSystemSD extends Behaviour {

    image = null;
    data = null;

    onStart() {
        //console.log(this)
        this.getData();
        this.parseArray();
        this.initGameObject();

    }

    getData() {
        this.image = gameProcess.image;
        this.data = gameProcess.gameData;
    }

    initGameObject() {
        const system = this.gameObject.getBehaviour(GameSystemSD);
        const parent = core.getObjectById("numberArray");
        const full = system.full;
        const unFull = system.unFull;

        let row = [];
        let array = [];

        for (let j = 1; j <= 5; j++) {
            row = [];
            for (let i = 1; i <= 5; i++) {
                const obj = new GameObject();
                const transform = new Transform();
                const clickButton = new ClickButton();
                obj.id = (i + "-" + j);
                transform.x = 73 * i - 73;
                transform.y = 73 * j - 73;
                if (full[j - 1][i - 1] == unFull[j - 1][i - 1]) {
                    clickButton.isChangable = false;
                    clickButton.number = full[j - 1][i - 1];
                } else {
                    clickButton.isChangable = true;
                }
                obj.addBehaviour(transform);
                obj.addBehaviour(clickButton);
                parent.addChild(obj);
                row[i - 1] = obj;
            }
            array[j - 1] = row;
        }
    }

    parseArray() {
        let full = [];
        let unFull = [];
        for (let child of this.data.full) {
            const row = child.split(",");
            full.push(row);
        }
        for (let child of this.data.unFull) {
            const row = child.split(",");
            unFull.push(row);
        }
        this.gameObject.getBehaviour(GameSystemSD).full = full;
        this.gameObject.getBehaviour(GameSystemSD).unFull = unFull;
    }
}

class ClickButton extends Button {


    number = "0";
    isChangable = false;

    onAddToGameObject() {
        super.onAddToGameObject();
        this.setImage(this.number);
    }

    onStart() {

        this.onClick = () => {
            if (this.isChangable) {
                const buttons = core.getObjectById("chooseNumber").children;
                for (let child of buttons) {
                    child.getBehaviour(NumberButton).object = this.gameObject;
                }
            }
        }
    }

    setImage(number) {
        this.number = number;
        switch (number) {
            case "0":
                this.image = "./image/数独/格.png";
                break;
            case "1":
                this.image = "./image/数独/数字1.png";
                break;
            case "2":
                this.image = "./image/数独/数字2.png";
                break;
            case "3":
                this.image = "./image/数独/数字3.png";
                break;
            case "4":
                this.image = "./image/数独/数字4.png";
                break;
            case "5":
                this.image = "./image/数独/数字5.png";
                break;
        }
    }
}

class NumberButton extends Button {

    @SerializedField(0)
    number = 0;
    object = null;


    onStart() {
        this.onClick = () => {
            //console.log(this.object)

            if (this.object) {
                const objectBehaviour = this.object.getBehaviour(ClickButton);
                objectBehaviour.setImage(this.number);
                const position = this.object.id.split("-");
                core.getObjectById("gameSystemSD").getBehaviour(GameSystemSD).unFull[position[1] - 1][position[0] - 1] = this.number;
            }
        }
    }
}

class SDFinishButton extends Button {

    onStart() {
        this.onClick = () => {
            gameProcess.setGameResult(core.getObjectById("gameSystemSD").getBehaviour(GameSystemSD).gameResult);
            gameProcess.changeSceneBack("morework");
        }

    }
}

class SDRestartButton extends Button {
    onStart() {
        this.onClick = () => {
            core.changeScene("./data/scenes/shudu.json");
        }
    }
}


core.registerBehaviourClass(GameSystemSD);
core.registerBehaviourClass(GameInitSystemSD);
core.registerBehaviourClass(SDRestartButton);
core.registerBehaviourClass(ClickButton);
core.registerBehaviourClass(SDFinishButton);
core.registerBehaviourClass(NumberButton);